#pragma once
#include "../Comm/comm.hpp"
#include "../Comm/util.hpp"
#include "../Comm/log.hpp"

namespace ns_model
{
    using namespace ns_util;
    using namespace ns_log;

    class Question
    {
    public:
        std::string _number; // 题目编号
        std::string _title;  // 题目标题
        std::string _star;   // 题目难度
        int _cpu_limit;      // 题目的CPU限制
        int _mem_limit;      // 题目的内存限制
        std::string _desc;   // 题目的描述
        std::string _header; // 题目的代码区域
        std::string _tail;   // 题目的测试用例
    };

    const std::string questions_list = "./questions/questions_list";
    const std::string questions_path = "./questions/";
    class Model
    {
    private:
        std::unordered_map<std::string, Question> questions;

    public:
        Model()
        {
            // 创建Modle对象时就完成对题库的加载
            assert(LoadQuestionList(questions_list));
        }
        ~Model() {}

    public:
        bool LoadQuestionList(const std::string &questions_list)
        {
            std::ifstream in(questions_list);
            if (!in.is_open())
            {
                LOG(ERROR) << "加载题库失败, 确定题库路径是否正确" << std::endl;
                return false;
            }

            std::string line;
            while (getline(in, line))
            {
                std::vector<std::string> token;
                std::string sep = " ";
                StringUtil::SplitString(line, &token, sep);
                if (token.size() != 5)
                {
                    LOG(ERROR) << "加载部分题目失效, 确定题目列表格式是否正确" << std::endl;
                    continue;
                }

                Question q;
                q._number = token[0];
                q._title = token[1];
                q._star = token[2];
                q._cpu_limit = atoi(token[3].c_str());
                q._mem_limit = atoi(token[4].c_str());
                FileUtil::ReadFromFile(questions_path + q._number + "/desc.txt", &(q._desc), true);
                FileUtil::ReadFromFile(questions_path + q._number + "/header.cpp", &(q._header), true);
                FileUtil::ReadFromFile(questions_path + q._number + "/tail.cpp", &(q._tail), true);

                questions.insert(std::make_pair(q._number, q));
            }
            LOG(INFO) << "加载题库成功" << std::endl;
            in.close();
            return true;
        }

        bool GetAllQuestions(std::vector<Question> *out)
        {
            if (questions.empty())
            {
                LOG(ERROR) << "用户获取题库失败" << std::endl;
                return false;
            }
            for (const auto &q : questions)
            {
                out->push_back(q.second);
            }
            LOG(INFO) << "用户获取题库成功" << std::endl;
            return true;
        }

        bool GetOneQuestion(const std::string &number, Question *q)
        {
            const auto &iter = questions.find(number);
            if (iter == questions.end())
            {
                LOG(ERROR) << "用户获取题目失败, 题目编号: " << number << std::endl;
                return false;
            }
            *q = iter->second;
            // LOG(INFO) << "用户获取题目成功, 题目编号: " << number << std::endl;
            return true;
        }
    };
} // namespace ns_model
